package com.jinsp.networklibrary.command;

import android.util.SparseArray;

import com.jinsp.networklibrary.bean.TCPBaseDataBean;

/**
 * Created by luoshuai on 1/15/22
 * <p>
 * TCP基础数据结构享元工厂
 */
public class TCPBaseDataBeanFactory {

    private final static SparseArray<TCPBaseDataBean<Object>> TCP_BASE_DATA_BEAN_SPARSE_ARRAY = new SparseArray<>();

    /**
     * 根据命令号获取TCPBaseDataBean对象
     *
     * @param commandNumber 命令号
     * @return 命令号对应的TCPBaseDataBean对象
     */
    public static TCPBaseDataBean<Object> getTCPBaseDataBean(int commandNumber) {
        TCPBaseDataBean<Object> tcpBaseDataBean = TCP_BASE_DATA_BEAN_SPARSE_ARRAY.get(commandNumber);
        if (tcpBaseDataBean == null) {
            tcpBaseDataBean = new TCPBaseDataBean<>();
            tcpBaseDataBean.setCommandNumber(commandNumber);
            TCP_BASE_DATA_BEAN_SPARSE_ARRAY.put(commandNumber, tcpBaseDataBean);
        }
        return tcpBaseDataBean;
    }

    /**
     * 释放所有数据对象
     */
    public static void release() {
        TCP_BASE_DATA_BEAN_SPARSE_ARRAY.clear();
    }
}
